﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Routing;
using Kooboo.CMS.Common.Runtime.Dependency;
using Kooboo.CMS.Sites.Extension.UI;
using Kooboo.CMS.Sites.Extension.UI.TopToolbar;
using Kooboo.Web.Mvc;

namespace DemoModule.UI
{
    [Dependency(typeof(IToolbarProvider), Key = "DemoModule.UI.BackButtonToolbar")]
    public class BackButtonToolbar : IToolbarProvider
    {
        public IEnumerable<ToolbarGroup> GetGroups(RequestContext requestContext)
        {
            return Enumerable.Empty<ToolbarGroup>();
        }

        public IEnumerable<ToolbarButton> GetButtons(RequestContext requestContext)
        {
            string skipRoutes = "Area,Controller,Action";
            var routeValues = requestContext.AllRouteValues();
            foreach (string key in skipRoutes.Split(','))
            {
                routeValues.Remove(key);
            }
            List<ToolbarButton> buttons = new List<ToolbarButton>();
            ToolbarButton testButton = new ToolbarButton
            {
                CommandText = "Back", //按钮显示文本
                IconClass = "cancel",
                HtmlAttributes = new Dictionary<string, object>()
            };
            if (routeValues.ContainsKey("return"))
            {
                testButton.HtmlAttributes["href"] = routeValues["return"];
            }
            else
            {
                testButton.HtmlAttributes["href"] = "javascript:;";
                testButton.HtmlAttributes["onclick"] = "javascript:history.go(-1)";
            }
            buttons.Add(testButton);
            return buttons;
        }

        public MvcRoute[] ApplyTo
        {
            get
            {
                return new[]
                {
                    new MvcRoute
                    {
                        Action = "Preview",
                        Controller = "Report",
                        Area = "DemoModule"
                    }, new MvcRoute
                    {
                        Action = "PublishDate",
                        Controller = "Report",
                        Area = "DemoModule"
                    }
                };
            }
        }
    }

}
